<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box{
            width: 500px;
            height: 100px;
            border: 2px solid rosybrown;
        }
        #sad{
            width: 500px;
            height: 100px;
            border: 2px solid rosybrown;
        }
    </style>
    <script>
    //1
        index =0;
        function copy() {
            var div = document.getElementById('box');
            div.innerText=''
            var cp = document.getElementById('content').value;
            var newH2=document.createElement('h2');
            var txt = document.createTextNode(cp);
            newH2.appendChild(txt);
            div.appendChild(newH2);
        }
    //2 有个button,有个h2(封校三天 快乐无边),当点击button时会显示文字，再次点击会文字会消失。
        function suprise() {
            if(index==0){
            var div = document.getElementById('sad');
            var newH2=document.createElement('h2')
            var txt = document.createTextNode('封校三天 快乐无边')
            newH2.appendChild(txt)
            div.appendChild(newH2)
            index++
            }else{
            var div = document.getElementById('sad');
            div.innerText=''
            index--
            }
        }
    </script>
</head>
<body>
    <input type="text" id="content">
    <button onclick="copy()" >复制</button>
    <div id="box">
    </div>

    <div>
        <button onclick="suprise()">点击有惊喜</button>
        <div id="sad">
        </div>
    </div>
</body>
</html>